﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.BLL;
using WXYW.DAL;

namespace WXYW
{
    public partial class EngineerWorkCondition : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Constants.QUERYSTR_STATION_ID))
            {
                if (!IsPostBack)
                {
                    BindEngineerList();
                    BindApplicationForm();
                }
            }
        }

        private void BindApplicationForm()
        {
            if (!string.IsNullOrEmpty(engineerListBox.SelectedValue))
            {
                ApplicationService service = new ApplicationService();
                applicationGridView.DataSource = service.GetProductNeedRepairByEngineer(Convert.ToInt32(engineerListBox.SelectedValue), "");
                applicationGridView.DataBind();
            }
        }

        private void BindEngineerList()
        {
            ServiceStationService sservice = new ServiceStationService();
            ServiceStation station = sservice.GetServiceStation(Convert.ToInt32(Request.QueryString[Constants.QUERYSTR_STATION_ID]));
            serviceStationLabel.Text = station.Name;

            UserService service = new UserService();
            engineerListBox.DataSource = service.GetSerciceStationEngineer(Convert.ToInt32(Request.QueryString[Constants.QUERYSTR_STATION_ID]));
            engineerListBox.DataValueField = "UserID";
            engineerListBox.DataTextField = "ContactPerson";
            engineerListBox.DataBind();
        }

        protected void applicationGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            if (e.NewPageIndex >= 0 && e.NewPageIndex < applicationGridView.PageCount)
            {
                applicationGridView.PageIndex = e.NewPageIndex;
            }
            
            BindApplicationForm();
        }

        protected void applicationGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem != null)
            {
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FFFF99'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");

                ((Label)e.Row.FindControl("IDLbl")).Text = (e.Row.RowIndex + 1).ToString();
            }
        }

        protected void engineerListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindApplicationForm();
        }
    }
}
